/*
 * taskMgrSub.S
 *
 *  Created on: May 1, 2015
 *      Author: mxw
 */

.global restore_run
restore_run:
    mov r3, r0          @back up r0 to r3
    add r0, r3, #64     @r0 = &g_tsk_mgr.arr[next_task_idx].r.cpsr
/* now condition is like this
                            offset
        +-----------------+
    r0->| next_tsk's cpsr |    64
        | next_tsk's pc   |    60
        | next_tsk's lr   |    56
        | next_tsk's sp   |    52
        | next_tsk's r12  |    48
        | next_tsk's r11  |    44
        | next_tsk's r10  |    40
        | next_tsk's r9   |    36
        | next_tsk's r8   |    32
        | next_tsk's r7   |    28
        | next_tsk's r6   |    24
        | next_tsk's r5   |    20
        | next_tsk's r4   |    16
        | next_tsk's r3   |    12
        | next_tsk's r2   |    8
        | next_tsk's r1   |    4
   r3-> | next_tsk's r0   |    0
        +-----------------+          */

    ldr r0, [r0]        @r0 = g_tsk_mgr.arr[next_task_idx].r.cpsr
    msr cpsr, r0        @cpsr = g_tsk_mgr.arr[next_task_idx].r.cpsr
    ldmia r3, {r0-r12, r13, r14, r15}
    
.global restore_run2
restore_run2:
    mov r3, r0          @back up r0 to r3
    add r0, r3, #64     @r0 = &g_tsk_mgr.arr[next_task_idx].r.cpsr
/* now condition is like this
                            offset
        +-----------------+
    r0->| next_tsk's cpsr |    64
        | next_tsk's pc   |    60
        | next_tsk's lr   |    56
        | next_tsk's sp   |    52
        | next_tsk's r12  |    48
        | next_tsk's r11  |    44
        | next_tsk's r10  |    40
        | next_tsk's r9   |    36
        | next_tsk's r8   |    32
        | next_tsk's r7   |    28
        | next_tsk's r6   |    24
        | next_tsk's r5   |    20
        | next_tsk's r4   |    16
        | next_tsk's r3   |    12
        | next_tsk's r2   |    8
        | next_tsk's r1   |    4
   r3-> | next_tsk's r0   |    0
        +-----------------+          */

    ldr r0, [r0]        @r0 = g_tsk_mgr.arr[next_task_idx].r.cpsr
    msr cpsr, r0        @cpsr = g_tsk_mgr.arr[next_task_idx].r.cpsr
    @开TIME1中断 TODO 1234 !!!!!
    ldmia r3, {r0-r12, r13, r14, r15}

.global set_task_sp
set_task_sp:
    mov sp, r0
    mov pc, lr

.global test_dis1
test_dis1:  @test_dis1(int* p)  output: p[0]=0x12345678; p[1]=0xAABBCCDD
    ldr r1, =0x12345678
    ldr r2, =0xAABBCCDD
    stmdb r0, {r1-r2}
    mov pc, lr

.global test_dis2
test_dis2:  @test_dis1(int* p)  output: p[0]=0x12345678; p[1]=0xAABBCCDD
    ldr r1, =0x12345678
    ldr r2, =0xAABBCCDD
    stmdb r0!, {r1-r2}
    mov pc, lr

.global call_swi_test @无调用者,仅作示例.备份
call_swi_test:
    mov	ip, sp
    stmdb sp!, {fp, ip, lr, pc}
    swi 0x20
    ldmia sp, {fp, sp, pc}
    @mov pc, lr

.global trigger_undef_instruction
trigger_undef_instruction:
    .word 0xFFFFFFFF @目前是这条指令优先使未定义指令发生


.global trigger_prefetch_abort
trigger_prefetch_abort:
    @bl 0xFFF0  @触发"未定义指令异常",原因不明 
    bkpt       @触发"未定义指令异常",原因不明   bkpt:break_point
@bkpt编译出的汇编如下 
@	e1200070 	bkpt	0x0000

.global trigger_data_abort
trigger_data_abort:
    mrc p15, 0, r0, c1, c0, 0 @request coproc 15 to perform operation 0 (type 0) on c1 and c0, and transfer the result back to r0
    orr r0, r0, #2
    mcr p15, 0, r0, c1, c0, 0   @设置地址对齐到4?
    mov r0, #0x1  @从非对齐到4的地址取数据   -> 数据异常发生
    ldr r0, [r0]
    mov pc, lr
